﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ES_Project.Model;
using System.Data;
using System.Data.SqlClient;

using System.Collections;
using System.Web.Security;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Threading;
using System.Globalization;
using System.Xml.Linq;

namespace ES_Project.Pages
{
    public partial class WebForm21 : System.Web.UI.Page
    {
        string StrConn = WebConfigurationManager.ConnectionStrings["baseessystem"].ToString();
        MySqlCommand objCmd = new MySqlCommand(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
               

                Binddata();
                Bindstudent_detail();
              
            }
        }
        private void clear()
        {
            TextBox2.Text = "";
            TextBox4.Text = "";
           
            TextBox33.Text = "";
            TextBox3.Text = "";
            TextBox11.Text = "";
            TextBox6.Text = "";
            TextBox8.Text = "";
            TextBox10.Text = "";
            TextBox7.Text = "";
            TextBox29.Text = "";
            TextBox21.Text = "";
            TextBox12.Text = "";
            TextBox13.Text = "";
            TextBox15.Text = "";
            TextBox19.Text = "";
            TextBox30.Text = "";
            TextBox31.Text = "";
            TextBox32.Text = "";
            TextBox14.Text = "";
            TextBox16.Text = "";
            TextBox20.Text = "";

            TextArea1.Value = "";
           

        }
        void Binddata()
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
           
            st_id.Text = Request.QueryString["st_id"];
            TextBox42.Text = Request.QueryString["register_date"];

            if (st_id.Text == "")
            {
                Response.Redirect("~/Pages/SearchEditStudent.aspx");
            }
            //data student_mast
            String strSQL;
            strSQL = "SELECT * FROM student_mast  where st_id = '" + this.st_id.Text + "' ";
            //strSQL = "SELECT * FROM student_detail  inner join cls_register_detail   on student_detail.cls_reg_detail_id = cls_register_detail.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id  where s WHERE   tbDCusCardS.CardSNo  ='" + this.Edit_st_id.Text + "' ";
            MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "student_mast");
            GridView1.DataSource = ds.Tables["student_mast"];
            GridView1.DataBind();
            da.Fill(ds);
            int _row = ds.Tables[0].Rows.Count;
            int i = 0;
            if (_row > i)
            {


                TextBox2.Text = (string)ds.Tables[0].Rows[i]["st_code"].ToString();
                TextBox3.Text = (string)ds.Tables[0].Rows[i]["st_name"].ToString();
                TextBox4.Text = (string)ds.Tables[0].Rows[i]["st_nickname"].ToString();
                //TextBox5.Text = (string)ds.Tables[0].Rows[i]["st_name"].ToString();
                RadioButtonList1.SelectedValue = (string)ds.Tables[0].Rows[i]["st_gender"].ToString();
                TextBox11.Text = (string)ds.Tables[0].Rows[i]["st_phone"].ToString();
                TextBox6.Text = (string)ds.Tables[0].Rows[i]["st_school"].ToString();
                TextBox8.Text = (string)ds.Tables[0].Rows[i]["st_birthday"].ToString();
                TextBox10.Text = (string)ds.Tables[0].Rows[i]["st_age"].ToString();
                TextBox7.Text = (string)ds.Tables[0].Rows[i]["st_email"].ToString();
                TextBox21.Text = (string)ds.Tables[0].Rows[i]["register_date"].ToString();
                TextBox12.Text = (string)ds.Tables[0].Rows[i]["st_end_date"].ToString();

                TextBox29.Text = (string)ds.Tables[0].Rows[i]["family_code"].ToString();
                TextArea1.Value = (string)ds.Tables[0].Rows[i]["objective"].ToString();

            }
            wherefamily();
           
        }
     
      
  void Bindstudent_detail()
        {
         MySqlConnection Conn = new MySqlConnection(StrConn);
            String strSQL;

            strSQL = "SELECT * FROM student_detail  inner join cls_register_detail   on student_detail.cls_reg_detail_id = cls_register_detail.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id  where st_id = '" + st_id.Text + "' ";
            MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "student_detail");
            GridView2.DataSource = ds.Tables["student_detail"];
            GridView2.DataBind();
    }
  protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {

      Button5.Enabled = true;


      using (essystemEntities context = new essystemEntities())
      {

          List<student_mast> sma = context.student_mast.Where("it.[st_id]=" + ((HiddenField)GridView1.SelectedRow.Cells[1].FindControl("HiddenField1")).Value.ToString() + "").ToList();
          if (sma.Count > 0)
          {
              List<student_mast> sm = context.student_mast.Where("it.[st_id]=" + ((HiddenField)GridView1.SelectedRow.Cells[1].FindControl("HiddenField1")).Value.ToString() + "").ToList();
              if (sm.Count > 0)
              {




                  TextBox2.Text = sm[0].st_code;
                  TextBox3.Text = sm[0].st_name;

                  //TextBox29.Text = sm[0].family_id.ToString();
                  TextBox4.Text = sm[0].st_nickname;
                  TextBox11.Text = sm[0].st_phone;
                  TextBox6.Text = sm[0].st_school;
                  TextBox10.Text = sm[0].st_age;
                  TextBox7.Text = sm[0].st_email;
                  TextBox21.Text = Convert.ToString(sm[0].register_date);
                  TextBox12.Text = Convert.ToString(sm[0].st_end_date);
                  RadioButtonList1.SelectedValue = sm[0].st_gender;
                  TextBox8.Text = Convert.ToString(sm[0].st_birthday);
                  TextArea1.Value = sm[0].objective;
                  st_id.Text = Convert.ToString(sm[0].st_id);

                  TextBox29.Text = sm[0].family_code;
                  //}
                  TextBox33.Text = sm[0].family_name;

                  TextBox14.Text = sm[0].mother_name;

                  TextBox30.Text = sm[0].parent_name;
                  TextArea2.Value = sm[0].address;

                  TextBox19.Text = sm[0].email_father;
                  TextBox15.Text = sm[0].phone_father;
                  TextBox20.Text = sm[0].email_mother;
                  TextBox16.Text = sm[0].phone_mother;
                  TextBox32.Text = sm[0].email_parent;
                  TextBox31.Text = sm[0].phone_parent;
                  TextBox13.Text = sm[0].father_name;

              }
          }
          wherefamily();

      }

  }
  void wherefamily()
  {
      using (essystemEntities context = new essystemEntities())
      {
          List<student_mast> chcfamily = context.student_mast.Where("it.[family_code]='" + TextBox29.Text + "'").ToList();
          if (chcfamily.Count() >= 1)
          {
              student_mast family = chcfamily[0];
              //Branchid.Text = Branch.BranchID.ToString();
              TextBox33.Text = family.family_name;
              TextBox13.Text = family.father_name;
              TextBox15.Text = family.phone_father;
              TextBox19.Text = family.email_father;
              TextBox14.Text = family.mother_name;
              TextBox16.Text = family.phone_mother;
              TextBox20.Text = family.email_mother;
              TextArea2.Value = family.address;

              TextBox30.Text = family.parent_name;
              TextBox31.Text = family.phone_parent;
              TextBox32.Text = family.email_parent;

          }
        
          ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Returnhidecode", "YAHOO.example.container.wait.hide();", true);
      }
  }
  protected void Button3_Click(object sender, EventArgs e)
  {
      MySqlConnection Conn = new MySqlConnection(StrConn);
      string strSQL;
      string a;
      if (TextBox37.Text != "")
      {
          a = TextBox37.Text;
      }
      else
      {
          a = DropDownList1.SelectedValue;
      }
      strSQL = "INSERT INTO student_detail (st_level,st_day,st_id,st_disc_rate,st_start,st_end,cls_reg_detail_id,st_hour)  " +
      " VALUES ('" + a + "','" + this.TextBox24.Text + "','" + this.st_id.Text + "','" + this.TextBox28.Text + "','" + this.TextBox25.Text + "','" + this.TextBox26.Text + "','" + Convert.ToInt32(TextBox36.Text) + "','" + this.TextBox39.Text + "')";

      MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
      Conn.Open();
      objCmd.Connection = Conn;
      objCmd.CommandType = CommandType.Text;
      objCmd.CommandText = strSQL;

      objCmd.ExecuteNonQuery();

      ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, "alert('บันทึกข้อมูลเรียบร้อยแล้วค่ะ')", true);
      Bindstid();
      cleargrid2();
  }
  void cleargrid2()
  {
      TextBox22.Text = "";
      TextBox23.Text = "";
      TextBox24.Text = "";
      TextBox25.Text = "";
      TextBox26.Text = "";
      TextBox28.Text = "";
      TextBox39.Text = "";
      DropDownList1.SelectedValue = null;

  }
  void Bindstid()
  {
      MySqlConnection Conn = new MySqlConnection(StrConn);
      String strSQL;
      //strSQL = "SELECT * FROM student_detail where st_id = '" + TextBox35.Text + "' ";
      strSQL = "SELECT * FROM student_detail  inner join cls_register_detail   on student_detail.cls_reg_detail_id = cls_register_detail.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id  where st_id = '" + st_id.Text + "' ";
      MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
      DataSet ds = new DataSet();
      da.Fill(ds, "student_detail");
      GridView2.DataSource = ds.Tables["student_detail"];
      GridView2.DataBind();
  }
  protected void Button9_Click(object sender, EventArgs e)
  {
    
      MySqlConnection objConn = new MySqlConnection();
     

      objConn = new MySqlConnection(StrConn);
      objConn.Open();
      string strSQL;
      strSQL = "UPDATE student_detail SET " +
  " st_day = '" + this.TextBox24.Text + "' " +
  " ,st_start = '" + this.TextBox25.Text + "' " +
   " ,st_end = '" + this.TextBox26.Text + "' " +
    " ,st_level = '" + this.DropDownList1.SelectedValue + "' " +
     " ,st_disc_rate = '" + this.TextBox28.Text + "' " +
      " ,cls_reg_detail_id = '" + this.TextBox38.Text + "' " +
        " ,st_hour = '" + this.TextBox39.Text + "' " +
  " WHERE st_detail_id = '" + this.st_detail_id.Text + "' ";

      objCmd = new MySqlCommand();
      objCmd.Connection = objConn;
      objCmd.CommandText = strSQL;
      objCmd.CommandType = CommandType.Text;

      objCmd.ExecuteNonQuery();

      Bindstid();

      cleargrid2();
  }

  protected void Button4_Click(object sender, EventArgs e)
  {
      cleargrid2();
  }

  protected void Button2_Click(object sender, EventArgs e)
  {
      clear();
  }


  void dropsubject()
  {
      //dropdownlist
      MySqlConnection Conn2 = new MySqlConnection(StrConn);

      string _query = "SELECT cls_course_no,cls_reg_detail_id FROM cls_register_mast inner join cls_register_detail on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id    WHERE cls_reg_code ='" + TextBox22.Text + "'";
      Conn2.Open();
      MySqlDataAdapter da2 = new MySqlDataAdapter(_query, Conn2);
      DataSet ds2 = new DataSet();
      da2.Fill(ds2);
      this.DropDownList1.DataSource = ds2;
      this.DropDownList1.DataTextField = "cls_course_no";
      this.DropDownList1.DataValueField = "cls_course_no";
      this.DropDownList1.DataBind();
      int _row2 = ds2.Tables[0].Rows.Count;
      int i2 = 0;
      if (_row2 > i2)
      {


          TextBox36.Text = (string)ds2.Tables[0].Rows[i2]["cls_reg_detail_id"].ToString();


      }
  }
  protected void Button8_Click(object sender, EventArgs e)
  {
      //รหัสวิชา
      MySqlConnection Conn = new MySqlConnection(StrConn);
      string strSQL;
   
      strSQL = "select * FROM student_detail  inner join cls_register_detail   on student_detail.cls_reg_detail_id = cls_register_detail.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id  WHERE cls_register_mast.cls_reg_code ='" + TextBox22.Text + "'";
      Conn.Open();
      MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
      DataSet ds4 = new DataSet();
      da4.Fill(ds4);

      int _row = ds4.Tables[0].Rows.Count;
      int i = 0;
      if (_row > i)
      {


          TextBox23.Text = (string)ds4.Tables[0].Rows[i]["cls_name"].ToString();
          TextBox38.Text = (string)ds4.Tables[0].Rows[i]["cls_reg_detail_id"].ToString();


      }
      else
      {
          TextBox23.Text = "ไม่มีข้อมูล";
      }
      dropsubject();
  }

  protected void Button6_Click(object sender, EventArgs e)
  {
      using (essystemEntities context = new essystemEntities())
      {
          List<student_mast> chcfamily = context.student_mast.Where("it.[family_code]='" + TextBox29.Text + "'").ToList();
          if (chcfamily.Count() >= 1)
          {
              student_mast family = chcfamily[0];
              //Branchid.Text = Branch.BranchID.ToString();
              TextBox33.Text = family.family_name;
              TextBox13.Text = family.father_name;
              TextBox15.Text = family.phone_father;
              TextBox19.Text = family.email_father;
              TextBox14.Text = family.mother_name;
              TextBox16.Text = family.phone_mother;
              TextBox20.Text = family.email_mother;
              TextArea2.Value = family.address;

              TextBox30.Text = family.parent_name;
              TextBox31.Text = family.phone_parent;
              TextBox32.Text = family.email_parent;

          }
            else
                {
                    TextBox33.Text = "";
                    TextBox13.Text = "";
                    TextBox15.Text = "";
                    TextBox19.Text = "";
                    TextBox14.Text = "";
                    TextBox16.Text = "";
                    TextBox20.Text = "";
                   TextArea2.Value = "";
                    TextBox30.Text = "";
                    TextBox31.Text = "";
                    TextBox32.Text = "";

                   
                }
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Returnhidecode", "YAHOO.example.container.wait.hide();", true);
            }
         
  }
  void searchclsname()
  {
      MySqlConnection Conn = new MySqlConnection(StrConn);
      string strSQL;
      //strSQL = "SELECT cls_reg_code,cls_name FROM cls_register_mast     WHERE cls_reg_code ='" + TextBox22.Text + "'";
      strSQL = "select * FROM student_detail  inner join cls_register_detail   on student_detail.cls_reg_detail_id = cls_register_detail.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id  WHERE cls_register_mast.cls_reg_code ='" + TextBox22.Text + "'";
      Conn.Open();
      MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
      DataSet ds4 = new DataSet();
      da4.Fill(ds4);

      int _row = ds4.Tables[0].Rows.Count;
      int i = 0;
      if (_row > i)
      {


          TextBox23.Text = (string)ds4.Tables[0].Rows[i]["cls_name"].ToString();
          TextBox38.Text = (string)ds4.Tables[0].Rows[i]["cls_reg_detail_id"].ToString();


      }
      else
      {
          TextBox23.Text = "ไม่มีข้อมูล";
      }
  }
  protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  {

      MySqlConnection Conn = new MySqlConnection(StrConn);
      string strSQL;
      strSQL = "SELECT * FROM student_detail WHERE st_detail_id ='" + GridView2.DataKeys[e.NewSelectedIndex].Value + "'";

      Conn.Open();
      MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
      DataSet ds4 = new DataSet();
      da4.Fill(ds4);

      int _row = ds4.Tables[0].Rows.Count;
      int i = 0;
      if (_row > i)
      {

          //Label4.Text = Convert.ToString(ds4.Tables[0].Rows[i]["family_id"]).ToString();
          TextBox24.Text = (string)ds4.Tables[0].Rows[i]["st_day"].ToString();
          st_detail_id.Text = Convert.ToString(ds4.Tables[0].Rows[i]["st_detail_id"]).ToString();

          TextBox25.Text = (string)ds4.Tables[0].Rows[i]["st_start"].ToString();
          TextBox26.Text = (string)ds4.Tables[0].Rows[i]["st_end"].ToString();
          TextBox28.Text = (string)ds4.Tables[0].Rows[i]["st_disc_rate"].ToString();
          TextBox39.Text = (string)ds4.Tables[0].Rows[i]["st_hour"].ToString();
          TextBox38.Text = (string)ds4.Tables[0].Rows[i]["cls_reg_detail_id"].ToString();
          DropDownList1.SelectedValue = (string)ds4.Tables[0].Rows[i]["st_level"].ToString();

      }

      MySqlConnection Conn2 = new MySqlConnection(StrConn);
      string strSQL2;
      //strSQL = "SELECT cls_reg_code,cls_name FROM cls_register_mast     WHERE cls_reg_code ='" + TextBox22.Text + "'";
      strSQL2 = "select * FROM student_detail  inner join cls_register_detail   on student_detail.cls_reg_detail_id = cls_register_detail.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id  WHERE student_detail.cls_reg_detail_id ='" + TextBox38.Text + "'";
      Conn2.Open();
      MySqlDataAdapter da2 = new MySqlDataAdapter(strSQL2, Conn2);
      DataSet ds2 = new DataSet();
      da2.Fill(ds2);

      int _row2 = ds2.Tables[0].Rows.Count;
      int i2 = 0;
      if (_row2 > i2)
      {

          TextBox22.Text = (string)ds2.Tables[0].Rows[i]["cls_reg_code"].ToString();
          TextBox23.Text = (string)ds2.Tables[0].Rows[i]["cls_name"].ToString();


      }
      dropsubject();
      searchclsname();
      Button3.Enabled = false;
  }

  protected void Button5_Click(object sender, EventArgs e)
  {
      using (essystemEntities context = new essystemEntities())
      {
          
                
                      List<student_mast> smast = context.student_mast.Where("it.[st_id]=" + st_id.Text + " ").ToList();
                      if (smast.Count() == 1)
                      {
                          student_mast smastEdit = context.student_mast.Where("it.[st_id]=" + st_id.Text + "").First();
                          if (smastEdit != null)
                          {
                              smastEdit.st_code = TextBox2.Text;
                              smastEdit.st_name = TextBox3.Text;
                              smastEdit.st_nickname = TextBox4.Text;
                              smastEdit.st_gender = RadioButtonList1.SelectedValue;
                              smastEdit.st_phone = TextBox11.Text;

                              smastEdit.st_age = TextBox10.Text;
                              smastEdit.st_email = TextBox7.Text;
                              smastEdit.register_date = Convert.ToDateTime(TextBox21.Text);
                              smastEdit.st_end_date = Convert.ToDateTime(TextBox12.Text);
                              smastEdit.st_school = TextBox6.Text;
                              smastEdit.objective = TextArea1.Value;
                              //smastEdit.family_id = Convert.ToInt32(TextBox29.Text);
                              if (!TextBox8.Text.Trim().Equals(""))
                              {
                                  smastEdit.st_birthday = Convert.ToDateTime(TextBox8.Text);
                              }
                              if (!TextBox29.Text.Trim().Equals(""))
                              {

                                  smastEdit.family_code = TextBox29.Text;
                              }
                                     smastEdit.family_name = TextBox33.Text;
                                     smastEdit.mother_name = TextBox14.Text;
                                     smastEdit.parent_name = TextBox30.Text;
                                  
                                     smastEdit.address = TextArea2.Value;
                                     smastEdit.email_father = TextBox19.Text;
                                     smastEdit.phone_father = TextBox15.Text;
                                     smastEdit.email_mother = TextBox20.Text;
                                     smastEdit.phone_mother = TextBox16.Text;
                                     smastEdit.email_parent = TextBox32.Text;
                                     smastEdit.phone_parent = TextBox31.Text;
                                     smastEdit.father_name = TextBox13.Text;
                                     afterfamilycode.Text = smastEdit.family_code;
                       
                                  //afteredit textbox
                                    TextBox29.Text = smastEdit.family_code;
                                    TextBox33.Text = smastEdit.family_name;
                                    TextBox30.Text = smastEdit.parent_name;
                                    TextBox31.Text = smastEdit.phone_parent;
                                    TextBox32.Text = smastEdit.email_parent;
                                    TextBox13.Text = smastEdit.father_name;
                                    TextBox15.Text = smastEdit.phone_father;
                                    TextBox19.Text = smastEdit.email_father;
                                    TextBox14.Text = smastEdit.mother_name;
                                    TextBox16.Text = smastEdit.phone_mother;
                                    TextBox20.Text = smastEdit.email_mother;
                                     TextArea2.Value = smastEdit.address;
                                  
                              if (context.SaveChanges() > 0)
                              {

                                  ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert2", "alert('แก้ไขข้อมูลเรียบร้อยแล้ว')", true);
                                  GridView1.DataBind();
                                  GridView1.SelectedIndex = -1;
                                
                                 
                                  clear();

                              }
                          }
                    


          }
      }
      Searchstidgridview1();
  }
  void Searchstidgridview1()
  {
      if (familycode.Text == "")
      {
          MySqlConnection Conn = new MySqlConnection(StrConn);
          String strSQL;
          strSQL = "SELECT * FROM Student_Mast  where Student_Mast.st_id = '" + st_id.Text + "' ";
          MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
          DataSet ds = new DataSet();
          da.Fill(ds, "Student_Mast");
          GridView1.DataSource = ds.Tables["Student_Mast"];
          GridView1.DataBind();
      }
      else
      {
          MySqlConnection Conn = new MySqlConnection(StrConn);
          String strSQL;
          strSQL = "SELECT * FROM Student_Mast left join family_mast on family_mast.family_code = student_mast.family_code where Student_Mast.family_code = '" + familycode.Text + "' or Student_Mast.family_code = '" + afterfamilycode.Text + "' and Student_Mast.register_date = '" + Convert.ToDateTime(TextBox42.Text).AddYears(-543).ToString("yyyy-MM-dd") + "' ";
          MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
          DataSet ds = new DataSet();
          da.Fill(ds, "Student_Mast");
          GridView1.DataSource = ds.Tables["Student_Mast"];
          GridView1.DataBind();
      }
  }
  protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {

     
      GridView2.PageIndex = e.NewPageIndex;
      Bindstid();
      GridView2.DataBind();
  }

  protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
      MySqlConnection Conn = new MySqlConnection(StrConn);
      string strSQL;
      strSQL = "DELETE FROM student_mast WHERE st_id ='" + GridView1.DataKeys[e.RowIndex].Value + "'";
      Conn.Open();
      objCmd = new MySqlCommand(strSQL, Conn);

      objCmd.ExecuteNonQuery();



      GridView1.EditIndex = -1;

     
      Searchstidgridview1();
  }

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          // loop all data rows
          foreach (DataControlFieldCell cell in e.Row.Cells)
          {
              // check all cells in one row
              foreach (Control control in cell.Controls)
              {
                  // Must use LinkButton here instead of ImageButton
                  // if you are having Links (not images) as the command button.
                  ImageButton button = control as ImageButton;
                  if (button != null && button.CommandName == "Delete")
                      // Add delete confirmation
                      button.OnClientClick = "if (!confirm('คุณต้องการที่จะลบข้อมูล?')) return;";
              }
          }
      }
  }

  protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
      MySqlConnection Conn = new MySqlConnection(StrConn);
      string strSQL;
      strSQL = "DELETE FROM student_detail WHERE st_detail_id ='" + GridView2.DataKeys[e.RowIndex].Value + "'";
      Conn.Open();
      objCmd = new MySqlCommand(strSQL, Conn);

      objCmd.ExecuteNonQuery();

      GridView2.EditIndex = -1;
      Bindstid();
  }

  protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          // loop all data rows
          foreach (DataControlFieldCell cell in e.Row.Cells)
          {
              // check all cells in one row
              foreach (Control control in cell.Controls)
              {
                  // Must use LinkButton here instead of ImageButton
                  // if you are having Links (not images) as the command button.
                  ImageButton button = control as ImageButton;
                  if (button != null && button.CommandName == "Delete")
                      // Add delete confirmation
                      button.OnClientClick = "if (!confirm('คุณต้องการที่จะลบข้อมูล?')) return;";
              }
          }
      }
  }

    }
}